# encoding=utf-8
import sys

import re

dtd_files = [
    '../resources/xmeml/dtd/v5.dtd'
]


attlist_lines = []

element_lines = []
#先过滤
for dtd_file in dtd_files:
    print dtd_file

    dtd = open(dtd_file,'r')
    print dtd

    for line in dtd.readlines():
        # print line

        pattern = re.compile(r'<!ELEMENT [^>]+>')

        match = pattern.search(line)

        if match:
            print match.group()
            element_lines.append(match.group())
        else:
            print "no match"


        pattern_attlist = re.compile(r'<!ATTLIST [^>]+>')
        match_attlist = pattern_attlist.search(line)

        if match_attlist:
            # print match_attlist.group()
            attlist_lines.append(match_attlist.group())

print '--- element lines ----'

#show lines
for element_line in element_lines:
    print element_line

print '--- attlist lines ----'

#show lines
for attlist_line in attlist_lines:
    print attlist_line

end_nodes = []
#find end node
# <!ELEMENT format (#PCDATA | samplecharacteristics | appspecificdata)*>
print "--- find end nodes ---"
for element_line in element_lines:
    if element_line == "<!ELEMENT format (#PCDATA | samplecharacteristics | appspecificdata)*>":
        print "format:" + element_line
    pattern_endnode = re.compile(r'<!ELEMENT [a-zA-Z0-9]+[ ]*[(]#PCDATA[^)^|]*[)][*]>')
    match = pattern_endnode.search(element_line)

    if match:
        print match.group()
        end_node_line = match.group()

        pattern_key = re.compile(r'<!ELEMENT [a-zA-Z0-9]*')
        match_key = pattern_key.search(end_node_line)

        if match_key:
            key = match_key.group()
            key = str(key).replace('<!ELEMENT ','')

            print key
            end_nodes.append(key)
    else:
        print element_line
        print False

#print endnodes
print '--- endnodes ---'
for endnode in end_nodes:
    print endnode



# class
print '--- class ----'
for element_line in element_lines:
    print "[element_line]:" + element_line

    pattern_key = re.compile(r'<!ELEMENT[ ][a-zA-Z0-9]*')
    match_key = pattern_key.search(element_line)

    if match_key:
        element_key = match_key.group()
        element_key = str(element_key).replace('<!ELEMENT ', '')
        print element_key

        check = False
        for end_node in end_nodes:
            if element_key == end_node:
                check = True

        if check == False:
            # is class
            print False
        else:
            print True


        # 如果不是end node，则是类，声明类
        if check == False:
            #content = '--- Class ' + element_key + ' ---' + '\n'
            content = 'package com.hava.film.timeline.xmeml.v5.entity;' + '\n'
            content += '\n'
            content += 'import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;' + '\n'
            content += 'import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;' + '\n'
            content += 'import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;' + '\n'
            content += '\n'
            content += 'import java.util.List;' + '\n'
            content += 'import java.util.ArrayList;' + '\n'
            content += '\n'
            content += '@JacksonXmlRootElement(localName = "' + element_key + '")' + '\n'
            content += 'public class ' + element_key.capitalize() + ' {' + '\n'
            content += '\n'

            # 检查是否有attr
            attr_check = False

            for attlist_line in attlist_lines:
                pattern_att = re.compile(r'<!ATTLIST[ ][a-zA-Z0-9]+[ ][a-zA-Z0-9]+')
                match_att = pattern_att.search(attlist_line)

                if match_att:
                    att = str(match_att.group()).replace('<!ATTLIST ','')
                    print att
                    key_value = att.split()
                    print key_value

                    if element_key == key_value[0]:
                        print "att:" + key_value[1]

                        content += '    ' + '//作为node的attr' + '\n'
                        content += '    ' + '@JacksonXmlProperty(isAttribute = true)' + '\n'
                        content += '    ' + 'public String ' + key_value[1] + ';' + '\n'
                        content += '\n'

            # 根据DTD的（）内的内容生成属性
            print '--- elements ---'
            pattern_list = re.compile(r'[(][a-zA-Z0-9 |#]+[)][*]')
            match_list = pattern_list.search(element_line)
            if match_list:
                print str(match_list.group())
                elements = str(match_list.group()).replace('(','').replace(')*','').replace(' ','')
                print elements
                elements = elements.split('|')
                print '[elements]:' + str(elements)

                for node in elements:
                    if node == "#PCDATA":
                        continue

                    content += '    ' + '//一个或者多个元素' + '\n'
                    content += '    ' + '@JacksonXmlProperty(localName = "' + node + '")' + '\n'
                    content += '    ' + '@JacksonXmlElementWrapper(useWrapping = false)' + '\n'

                    end_node_check = False
                    for end_node in end_nodes:
                        if end_node == node:
                            end_node_check = True

                    if end_node_check == False:
                        content += '    ' + 'public List<' + node.capitalize() + '> ' + node + ' = new ArrayList<' + node.capitalize() + '>();' + '\n'
                    else:
                        content += '    ' + 'public List<String> ' + node + ' = new ArrayList<String>();' + '\n'

                    content += '\n'



            # content +=
            content += '}' + '\n'
            # content += '---end---'
            print content

            file = open('../java/com/hava/film/timeline/xmeml/v5/entity/' + element_key.capitalize() + '.java','w')
            file.write(content)
            file.close()